﻿
update = 0;                 //更新数据标志 1：更新音量大小

var page = {
    musicData: 0,           //接收上界面传来的btn_id
    urlList: 0,             //播放url路径    
    nameList: 0,            //歌曲名称列表
    pictList: 0,            //歌曲图片列表
    isPlay: false,          //播放选项
    playIndex: 0,           //音乐列表索引
    timer: 0,               //processbar timer
    totalTime: 0,           //播放时长
    currentTime: 0,
    process_data: 0,        //播放进度条    
    process_data2: 0,
    timer: 0,               //processbar timer
    globalVolume: 20,       //音量初始值--滑块

    musicArray: [
        { src1: "/mnt/sd0/filesystem/shinian.mp3", src2: "/mnt/sd0/filesystem/Yummy.mp3", src3: "/mnt/sd0/filesystem/Winter Without You.mp3", src4: "/mnt/sd0/filesystem/Cold water.mp3" },
        { src1: "/mnt/sd0/filesystem/haikuo.mp3", src2: "/mnt/sd0/filesystem/Habitual.mp3", src3: "/mnt/sd0/filesystem/Robin Hood.mp3", src4: "/mnt/sd0/filesystem/Holy.mp3" },
        { src1: "/mnt/sd0/filesystem/youhe.mp3", src2: "/mnt/sd0/filesystem/E.T.A.mp3", src3: "/mnt/sd0/filesystem/shaonv.mp3", src4: "/mnt/sd0/filesystem/Home to Mama.mp3" },
        { src1: "/mnt/sd0/filesystem/Intentions.mp3", src2: "/mnt/sd0/filesystem/All Around Me.mp3", src3: "/mnt/sd0/filesystem/qifeng.mp3", src4: "/mnt/sd0/filesystem/Monster.mp3" },
    ],

    musicName: [
        { name1: "十年-陈奕迅", name2: "Yummy-Justin Bieber", name3: "Winter Without You-Gloria Kim", name4: "Cold water-Justin Bieber" },
        { name1: "海阔天空-Beyond", name2: "Habitual-Justin Bieber", name3: "Robin Hood-Anson Seabra", name4: "Holy-Justin Bieber" },
        { name1: "有何不可-许嵩", name2: "E.T.A-Justin Bieber", name3: "少女的祈祷-杨千嬅", name4: "Home to Mama-Justin Bieber" },
        { name1: "Intentions-Justin Bieber", name2: "All Around Me-Justin Bieber", name3: "起风了-买辣椒也用券", name4: "Monster-Justin Bieber" },
    ],

    musicPicture: [
        { picture1: "shinian.png", picture2: "yummy.png", picture3: "winter.png", picture4: "water.png" },
        { picture1: "weizhi.png", picture2: "change.png", picture3: "Robin.png", picture4: "holy.png" },
        { picture1: "youhe.png", picture2: "change.png", picture3: "shaonv.png", picture4: "homema.png" },
        { picture1: "intent.png", picture2: "change.png", picture3: "qifeng.png", picture4: "master.png" },
    ],

    /* 此方法在第一次显示窗体前发生 */
    onLoad: function (event) {
        this.audioEvenDeinit();
        this.musicData = event;
        this.loadmusic(parseInt(this.musicData));
        this.audioEventInit();
    },

    /***************music List Item******************/
    loadmusic: function (data) {
        this.urlList = new Array();     //存放音乐数组
        this.nameList = new Array();    //存放音乐名数组
        this.pictList = new Array();     //存放音乐图片数组
        var musicList = new Array();

        for (var i = 0, len = this.musicArray.length; i < len; i++) {
            var musicItem = new Object();
            musicItem.label1 = new Object();
            switch (data) {
                case 1:
                    //console.log(this.musicArray[i].src1.indexOf(".mp3"));
                    musicItem.label1.value = this.musicName[i].name1;
                    this.urlList.push(this.musicArray[i].src1);
                    this.nameList.push(this.musicName[i].name1);
                    this.pictList.push(this.musicPicture[i].picture1);
                    break;
                case 2:
                    musicItem.label1.value = this.musicName[i].name2;
                    this.urlList.push(this.musicArray[i].src2);
                    this.nameList.push(this.musicName[i].name2);
                    this.pictList.push(this.musicPicture[i].picture2);
                    break;
                case 3:
                    musicItem.label1.value = this.musicName[i].name3;
                    this.urlList.push(this.musicArray[i].src3);
                    this.nameList.push(this.musicName[i].name3);
                    this.pictList.push(this.musicPicture[i].picture3);
                    break;
                case 4:
                    musicItem.label1.value = this.musicName[i].name4;
                    this.urlList.push(this.musicArray[i].src4);
                    this.nameList.push(this.musicName[i].name4);
                    this.pictList.push(this.musicPicture[i].picture4);
                    break;
            }
            musicItem.imagebox1 = new Object();
            musicItem.imagebox1.id = "musicImage" + i;   //设置每一个列表前的图标id
            musicList.push(musicItem);
            console.log("=================>" + musicItem.imagebox1.id);
        }

        this.setData({
            AlbumList: {
                list: {
                    page: this,
                    items: [{
                        xml: "Panels/musicListItem",
                        items: musicList
                    }]
                }
            }
        })
    },

    //audio事件
    audioEventInit: function () {
        var that = this;
        audio.onPlay(function (e) {     //监听音频播放 e:时长
            that.isPlay = true;
            that.setData({ Switch1: { value: true } })
            that.totalTime = e;
        });
        audio.onPause(function (e) {    //监听音频暂停
            console.log("==> onPause : " + e);
        });
        audio.onEnded(function (e) {    //监听音频播放结束
            console.log("==> onEnded : " + e);
            that.setData({ Switch1: { value: false } });
            that.process_data = 0;
            that.playIndex += 1;        //播放完毕播放下一曲
            that.playMusic();
        });
        audio.onTimeUpdate(function (e) {//监听音频播放进度
            /*刷新进度条*/
            var time = parseInt(e / 1000);
            if (that.currentTime != time) {
                that.process_data = time;
                that.process_data2 = time;
                console.log("@@==>audio time:" + time);
                that.currentTime = time;
            }
        });
        audio.onError(function (e) {     //监听音频播放错误
            console.log("==> onError : " + e);
            audio.stop();
        });
        audio.onSeeked(function (e) {    //监听音频完成 seek 操作
            console.log("==> onSeeked : " + e);
        });
        audio.onStop(function (e) {      //监听音频停止
            console.log("==> onStop : " + e);
            that.setData({ Switch1: { value: false } });
            that.process_data = 0;       //刷新进度条
        });
        this.changeIndex(this.playIndex);//下一曲
    },

    /**************************************************************改********************************************************** */

    //切换下一曲--列表下移动画
    changeIndex: function (targetId) {
        var data = new Object();
        data["musicImage" + targetId] = { value: "icon_playing.png" };
        this.setData(data);
        this.setData({ AlbumList: { move: 30 * targetId } })
        console.log("@@targetId======>:" + targetId);

        var music_name = this.nameList[targetId];
        var music_picture = this.pictList[targetId];
        this.setData({ music_item: { value: music_name } });  //音乐名称展示在标题
        this.setData({ picture: { value: music_picture } });  //音乐名称展示在标题
    },

    onPlay: function (e) {
        var targetId = parseInt(e.target.id.substring(10));
        console.log("@@=====>onPlay is click---targetId:" + targetId + "---line:133");
        var data = new Object();
        data["musicImage" + this.playIndex] = { value: "icon_play.png" };
        this.setData(data);
        this.changeIndex(targetId);
        this.playIndex = targetId;
        if (this.isPlay) {
            this.playMusic();
        }
    },

    //播放音乐
    playMusic: function () {
        if (this.isPlay == false) {
            this.isPlay = true;
            this.setData({ Switch1: { value: true } })
        }
        if (this.playIndex < 0) {
            this.playIndex = this.urlList.length - 1;
        } else if (this.playIndex >= this.urlList.length) {
            this.playIndex = 0;
        }

        // set url
        var url = this.urlList[this.playIndex];
        console.log("@@The music url is:" + url);
        audio.stop();
        audio.setSrc(url);
        if (this.isPlay) {
            audio.play();
        } else {
            this.isPlay = false;
            this.setData({ Switch1: { value: false } });
        }
    },

    //暂停/播放--按钮回调
    onSwitch: function (event) {
        this.isPlay = event.detail.value;
        if (this.isPlay) {
            this.playMusic();
        } else {
            audio.pause();
        }
    },

    //切换歌曲--下一首/上一首--按钮回调
    onTap: function (event) {
        switch (event.target.id) {
            case "before":
                audio.stop();
                var data = new Object();
                data["musicImage" + this.playIndex] = { value: "icon_play.png" };
                this.setData(data);
                this.playIndex--;
                if (this.playIndex < 0) {
                    this.playIndex = 0;
                }
                this.changeIndex(this.playIndex);
                if (this.isPlay) {
                    this.playMusic();
                }
                break;
            case "next":
                audio.stop();
                var data = new Object();
                data["musicImage" + this.playIndex] = { value: "icon_play.png" };
                this.setData(data);
                this.playIndex++;
                if (this.playIndex >= this.urlList.length) {
                    this.playIndex = this.urlList.length - 1;
                }
                this.changeIndex(this.playIndex);
                if (this.isPlay) {
                    this.playMusic();
                }
                break;
        }
    },

    //滑动条改变音量
    onVolumeChange: function (event) {
        globalVolume = event.detail.value;
        update = 1;
    },

    //audio初始化
    audioEvenDeinit: function () {
        audio.offPlay();    //取消监听音频播放事件
        audio.offPause();   //取消监听音频暂停事件
        audio.offEnded();
        audio.offTimeUpdate();
        audio.offError();
        audio.offSeeked();
        audio.offStop();
        audio.stop();
    },

    /* 此方法展示窗体后发生 */
    onResume: function (event) {

    },

    /* 当前页状态变化为显示时触发 */
    onShow: function (event) {
        var that = this;
        that.timer = setInterval(function () {
            if (update == 1) {
                audio.setVolume(globalVolume);
                update = 0;
            }
            if (that.process_data > that.process_data2 + 1) {
                that.setData({ progressbar1: { value: 0 } });
                that.process_data = 0;
            } else {
                /*更新progressbar的进度和label的显示文本*/
                that.setData({ progressbar1: { value: that.process_data } });
            }
        }, 200)
    },

    /* 当前页状态变化为隐藏时触发 */
    onHide: function (event) {

    },

    /* 此方法关闭窗体前发生 */
    onExit: function (event) {
        if (this.timer != 0) {
            clearInterval(this.timer);
        }
        this.audioEvenDeinit();
    },

    onBack: function (event) {
        pm.navigateBack();
        this.audioEvenDeinit();
    },
};

Page(page);

page = 0;
